#include <iostream>

#include <signal.h>

#include "../comm/httplib.h"
#include "oj_control.hpp"

#include "user.hpp"
#include "selfjudge.hpp"
#include "cookie.hpp"
#include "../comm/util.hpp"
#include "on_insert_question.hpp"

#include "register.hpp"
#include "verify.hpp"
#include "login.hpp"
#include "user_center.hpp"

#include "forgetpassword.hpp"
#include "verify_forgetpassword.hpp"

using namespace httplib;
using namespace ns_control;
using namespace ns_util;

using namespace std;

static Control *ctrl_ptr = nullptr;

void Recovery(int signno)
{
    ctrl_ptr->Recovery();
}

void GetUnauthorized(string *html)
{
    FileUtil::ReadFile("wwwroot/unauthorized.html", html, true);
}

int main()
{
    // 这个信号用于将所有的主机上线
    signal(SIGINT, Recovery);

    // 用户请求的服务路由功能
    Server svr;

    // 这两个接口，用户注册和登录（原始的登录注册、暂时不用）
    svr.Post("/register", handle_register);
    svr.Post("/login", handle_login);

    // 提供在线编译功能
    svr.Post("/compile", handle_compile);

    Control ctrl;
    ctrl_ptr = &ctrl;

    // 获取所有的题目列表
    svr.Get("/all_questions", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
        if (!check_cookie(req)) {
            resp.status = 401; // 未授权
            string html;
            GetUnauthorized(&html);
            resp.set_content(html, "text/html;charset=utf-8");
            return;
        }

        // 返回含有所有题目的 HTML 网页
        std::string html;
        ctrl.AllQuestions(&html);
        resp.set_content(html, "text/html;charset=utf-8"); });

    // 用户要根据题目编号，获取题目内容
    svr.Get(R"(/question/(\d+))", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
        if (!check_cookie(req)) {
            resp.status = 401; // 未授权
            string html;
            GetUnauthorized(&html);
            resp.set_content(html, "text/html;charset=utf-8");
            return;
        }
        std::string number = req.matches[1];
        std::string html;
        ctrl.Question(number, &html);
        resp.set_content(html, "text/html;charset=utf-8"); });

    // 用户提交代码，进行判题
    svr.Post(R"(/judge/(\d+))", [&ctrl](const httplib::Request &req, httplib::Response &resp)
             {
        if (!check_cookie(req)) {
            resp.status = 401; // 未授权
            string html;
            GetUnauthorized(&html);
            resp.set_content(html, "text/html;charset=utf-8");
            return;
        }
        std::string number = req.matches[1];
        std::string result_json;
        ctrl.Judge(number, req.body, &result_json);
        resp.set_content(result_json, "application/json;charset=utf-8"); });

    // 用户获取题解
    svr.Get(R"(/answer/(\d+))", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
        if (!check_cookie(req)) {
            resp.status = 401; // 未授权
            string html;
            GetUnauthorized(&html);
            resp.set_content(html, "text/html;charset=utf-8");
            return;
        }
        std::string number = req.matches[1];
        std::string html;
        ctrl.Answer(number, &html);
        resp.set_content(html, "text/plain;charset=utf-8"); });

    // 获取首页（bch.html）
    svr.Get("/bch", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
        if (!check_cookie(req)) {
            resp.status = 401; // 未授权
            string html;
            GetUnauthorized(&html);
            resp.set_content(html, "text/html;charset=utf-8");
            return;
        }
        // 返回 bch.html 的 HTML 网页
        std::string html;
        FileUtil::ReadFile("wwwroot/bch.html",&html,true);
        resp.set_content(html, "text/html;charset=utf-8"); });

    // 获取登录页面（RL.html）
    svr.Get("/RL", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
        // 返回 bch.html 的 HTML 网页
        std::string html;
        FileUtil::ReadFile("wwwroot/RL.html",&html,true);
        resp.set_content(html, "text/html;charset=utf-8"); });

    // 获取在线编程页面admin.html
    svr.Get("/admin", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
        if (!check_cookie(req)) {
            resp.status = 401; // 未授权
           string html;
            GetUnauthorized(&html);
            resp.set_content(html, "text/html;charset=utf-8");
            return;
        }
        // 返回 bch.html 的 HTML 网页
        std::string html;
        FileUtil::ReadFile("wwwroot/admin.html",&html,true);
        resp.set_content(html, "text/html;charset=utf-8"); });

    // 在线录题服务
    svr.Post("/submit", insert_into_db);

    // 注册服务
    //  svr.Post("/register_test", handle_register_test);
    svr.Post("/register_test", [&ctrl](const httplib::Request &req, httplib::Response &resp)
             {
            cout<<"req.body:" <<req.body << endl;
            handle_register_test(req,resp); });

    // 身份验证
    svr.Post("/verify", handle_verify);

    svr.Post("/forgetpassword", [&ctrl](const httplib::Request &req, httplib::Response &resp)
             {
            cout<<"req.body:" <<req.body << endl;
            handle_register_test_fp(req,resp); });

    // 身份验证
    svr.Post("/verify_forgetpassword", handle_verify_fp);

    // 登录
    svr.Post("/login_test", handle_login_test);

    // 用户中心
    svr.Get("/user_center", [&ctrl](const httplib::Request &req, httplib::Response &resp)
            {
    // 检查用户的 Cookie
    if (!check_cookie(req))
    {
        resp.status = 401; // 未授权
        string html;
        GetUnauthorized(&html);
        resp.set_content(html, "text/html;charset=utf-8");
        return;
    }
    handle_user_center(req,resp); });

    // 设置首页的内容
    svr.set_base_dir("./wwwroot"); // 设置静态资源的根目录

    svr.listen("0.0.0.0", 8080);

    return 0;
}
